package com.jshoperxms.controller.mall.backstage.goods;


import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.model.SearchGoodsTypeTModel;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.GoodsTypeT;
import com.jshoperxms.service.GoodsTypeTService;
import com.jshoperxms.service.impl.Serial;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;


@Controller
@RequestMapping("/mall/goods/goodstype")
public class GoodsTypeTAction extends BaseTController{
	@Resource
	private GoodsTypeTService goodsTypeTService;

	private Map<String, Object> responsedata = new HashMap<>();//数据形式


	/**
	 * 检查是否有重名商品类型名称
	 * @return
	 */
	private boolean checkGoodsTypeName(String name){
		if(StringUtils.isBlank(name)){
			return false;
		}
		Criterion criterion=Restrictions.eq("name",name);
		GoodsTypeT goodsTypeT=this.goodsTypeTService.findOneByCriteria(GoodsTypeT.class, criterion);
		if(goodsTypeT!=null){
			return false;
		}
		return true;
	}
	
	
	/**
	 * 查询所有的商品类型
	 * @return
	 */
	@RequestMapping(value="/findAll",method= RequestMethod.POST)
	public ResponseEntity findAllGoodsTypeT(){
		Criterion criterion=Restrictions.eq("status", BaseEnums.DataUsingState.USING.getState());
		List<GoodsTypeT>list=this.goodsTypeTService.findByCriteria(GoodsTypeT.class, criterion,Order.desc("updatetime"));
		if(!list.isEmpty()){
			responsedata.put("beanlist",list);
			responsedata.put("sucflag", true);
		}
		return Response.success(responsedata);
	}
	/**
	 * 保存商品类型和参数
	 * @return
	 */
	@RequestMapping(value="/save",method= RequestMethod.POST)
	public ResponseEntity saveGoodsTypeT( @RequestBody GoodsTypeT gt){
		if(StringUtils.isNotBlank(gt.getName())){
			if(checkGoodsTypeName(gt.getName())){
				GoodsTypeT gtn=new GoodsTypeT();
				gtn.setGoodstypeid(this.serial.Serialid(Serial.GOODSTYPE));
				gtn.setName(StringUtils.trim(gt.getName()));
				gtn.setGoodsParameter(gt.getGoodsParameter());
				gtn.setCreatetime(BigInteger.valueOf(DateTools.getSystemTimeLong()));
				gtn.setCreatorid(BaseTools.getAdminCreateId());
				gtn.setStatus(gt.getStatus());
				gtn.setUpdatetime(BigInteger.valueOf(DateTools.getSystemTimeLong()));
				gtn.setVersiont(1);
				this.goodsTypeTService.save(gtn);
				responsedata.put("sucflag", true);
			}
		}else{
			responsedata.put("sucflag",false);
			responsedata.put("msg",WarnWrods.ERRPR_PARAMS);
		}
		return Response.success(responsedata);
	}
	
	/**
	 * 获取所有商品类型 1，区分是否有排序，是否有搜索，然后进行相应的业务方法调用
	 * 
	 * @return
	 */
	@RequestMapping(value="/findByPage",method= RequestMethod.POST)
	public ResponseEntity findGoodsTypeTByPage(SearchGoodsTypeTModel stm, @RequestParam("search[value]") String search) {
		//检查是否有自定义搜索内容 1标示进行自定义搜索逻辑
		findCustomSearchGoodsTypeT(stm,search);
		return Response.success(responsedata);
	}

	public void processGoodsTypeTList(List<GoodsTypeT> list) {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		for (Iterator<GoodsTypeT> it = list.iterator(); it.hasNext();) {
			GoodsTypeT gtn = it.next();
			Map<String, Object> cellMap = new HashMap<String, Object>();
			cellMap.put("id", gtn.getGoodstypeid());
			cellMap.put("name", gtn.getName());
			cellMap.put("status", BaseEnums.DataUsingState.getName(gtn.getStatus()));
			cellMap.put("updatetime", DateTools.formateLongDateToString(gtn.getUpdatetime().longValue()));
			cellMap.put("version", gtn.getVersiont());
			data.add(cellMap);
		}
		responsedata.put("data",data);
	}


	/**
	 * 根据search[value]过滤查询
	 * @param search
	 */
	private void findSearchGoodsTypeT(SearchGoodsTypeTModel stm,String search) {

		int currentPage=1;
		if(stm.getStart()!=0){
			currentPage=stm.getStart()/stm.getLength()==1?2:stm.getStart()/stm.getLength()+1;
		}
		int lineSize = stm.getLength();
		Criterion criterion;
		Criterion criterion1=Restrictions.ne("status", BaseEnums.DataUsingState.DEL.getState());
		Criterion criterion2=Restrictions.like("name",search,MatchMode.ANYWHERE);
		criterion=Restrictions.and(criterion1).add(criterion2);
		int records=this.goodsTypeTService.count(GoodsTypeT.class,criterion).intValue();
		stm.setRecordsTotal(records);
		stm.setRecordsFiltered(records);
		responsedata.put("recordsTotal",records);
		responsedata.put("recordsFiltered",records);
		List<GoodsTypeT> list = this.goodsTypeTService.findByCriteriaByPage(
					GoodsTypeT.class, criterion,Order.desc("updatetime"), currentPage,
					lineSize);
		this.processGoodsTypeTList(list);

	}


	/**
	 * 根据自定义搜索内容查询
	 * 1,根据名称搜索
	 * @param stm
     */
	private void findCustomSearchGoodsTypeT(SearchGoodsTypeTModel stm,String search){
		//如果有自定义搜索
		if(stm.getIsCustomSearch()==1){
			int currentPage=1;
			if(stm.getStart()!=0){
				currentPage=stm.getStart()/stm.getLength()==1?2:stm.getStart()/stm.getLength()+1;
			}
			int lineSize = stm.getLength();
			Criterion criterion;
			Criterion criterion1=Restrictions.ne("status", BaseEnums.DataUsingState.DEL.getState());
			Criterion criterion2=null;
			if(StringUtils.isNotBlank(stm.getName())){
				criterion2=Restrictions.like("name",stm.getName(), MatchMode.ANYWHERE);
			}
			if(StringUtils.isNotBlank(search)){
				criterion2=Restrictions.like("name",search,MatchMode.ANYWHERE);
			}
			if(criterion2!=null){
				criterion=Restrictions.and(criterion1).add(criterion2);
				int records=this.goodsTypeTService.count(GoodsTypeT.class,criterion).intValue();
				stm.setRecordsTotal(records);
				stm.setRecordsFiltered(records);
				responsedata.put("recordsTotal",records);
				responsedata.put("recordsFiltered",records);
				List<GoodsTypeT> list = this.goodsTypeTService.findByCriteriaByPage(
						GoodsTypeT.class, criterion,Order.desc("updatetime"), currentPage,
						lineSize);
				this.processGoodsTypeTList(list);
			}
		}else{
			if(StringUtils.isNotBlank(search)){
				findSearchGoodsTypeT(stm,search);
			}else{
				findDefaultAllGoodsTypeT(stm);
			}
		}


	}

	/**
	 * 默认查询
	 * @param stm
     */
	private void findDefaultAllGoodsTypeT(SearchGoodsTypeTModel stm) {
		int currentPage=1;
		if(stm.getStart()!=0){
			currentPage=stm.getStart()/stm.getLength()==1?2:stm.getStart()/stm.getLength()+1;
		}
		int lineSize = stm.getLength();
		Criterion criterion=Restrictions.ne("status", BaseEnums.DataUsingState.DEL.getState());
		int records=this.goodsTypeTService.count(GoodsTypeT.class,criterion).intValue();
		stm.setRecordsFiltered(records);
		stm.setRecordsTotal(records);
		responsedata.put("recordsTotal",records);
		responsedata.put("recordsFiltered",records);
		List<GoodsTypeT> list = this.goodsTypeTService.findByCriteriaByPage(
				GoodsTypeT.class, criterion,Order.desc("updatetime"), currentPage,
				lineSize);
		this.processGoodsTypeTList(list);
	}
	
	
	/**
	 * 根据商品类型id查询内容
	 * @return
	 */
//	@Action(value = "find", results = { @Result(name = "json", type = "json") })
//	public String findOneGoodsTypeT(){
//		if(StringUtils.isBlank(this.getGoodstypeid())){
//			return JSON;
//		}
//		GoodsTypeT goodsTypeT=this.goodsTypeTService.findByPK(GoodsTypeT.class, this.getGoodstypeid());
//		if(goodsTypeT!=null){
//			bean=goodsTypeT;
//			this.setSucflag(true);
//		}
//		return JSON;
//	}
	
	/**
	 * 更新商品类型参数
	 * @return
	 */
//	@Action(value = "update", results = { @Result(name = "json", type = "json") })
//	public String updateGoodsTypeT(){
//		if(StringUtils.isBlank(this.getGoodstypeid())){
//			return JSON;
//		}
//		GoodsTypeT gtn=this.goodsTypeTService.findByPK(GoodsTypeT.class, this.getGoodstypeid());
//		gtn.setName(StringUtils.trim(this.getName()));
//		gtn.setGoodsParameter(this.getGoodsparameter());
//		gtn.setStatus(this.getStatus());
//		gtn.setCreatorid(BaseTools.getAdminCreateId());
//		gtn.setUpdatetime(DateTools.getSystemTime());
//		gtn.setVersiont(gtn.getVersiont()+1);
//		this.goodsTypeTService.update(gtn);
//		this.setSucflag(true);
//		return JSON;
//	}

	/**
	 * 删除商品类型参数
	 * @return
	 */
//	@Action(value = "del", results = { @Result(name = "json", type = "json") })
//	public String delGoodsTypeT(){
//		String ids[]=StringUtils.split(this.getIds(), TagWords.SPLITDOT);
//		for(String s:ids){
//			GoodsTypeT goodsTypeT=this.goodsTypeTService.findByPK(GoodsTypeT.class, s);
//			if(goodsTypeT!=null){
//				goodsTypeT.setStatus(DataUsingState.DEL.getState());
//				goodsTypeT.setUpdatetime(DateTools.getSystemTime());
//				this.goodsTypeTService.update(goodsTypeT);
//			}
//		}
//		this.setSucflag(true);
//		return JSON;
//	}
//

	
}
